// $Id: PlaceShape.cs 124 2007-05-28 15:51:51Z xavex2k $

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Diagram.Handles;
using Diagram.View;

namespace PetriDiagram.Shapes
{
    [Serializable]
    public class PlaceShape : NetElementShape
    {
        private Size size;
        private const int tokenSize = 8;
        private Point[] tokenOffsets;

        public PlaceShape()
        {
            size = new Size( 30, 30 );
            Bounds = Rectangle.Empty;
            Cursor = new Cursor( new MemoryStream( Resource.PlaceCursor ) );

            tokenOffsets = new Point[]
                {
                    new Point( 11, 11 ),
                    new Point( 4, 4 ), 
                    new Point( 18, 18 ), 
                    new Point( 18, 4 ), 
                    new Point( 4, 18 ), 
                };
        }

        public override Shape Clone()
        {
            return new PlaceShape();
        }

        public override void Draw( Graphics g )
        {
            g.FillEllipse( Brushes.White, Bounds );
            g.DrawEllipse( Pens.Black, Bounds );

            if( Place.Tokens <= 5 )
            {
                for( int i = 0; i < Place.Tokens; i++ )
                {
                    Rectangle rect = new Rectangle( Bounds.Location, new Size( tokenSize, tokenSize ) );
                    rect.Offset( tokenOffsets[ i ] );

                    g.FillEllipse( Brushes.Black, rect );
                }
            }
            else
            {
                SizeF s = g.MeasureString( Place.Tokens.ToString(), Settings.Default.NameFont );

                g.DrawString( Place.Tokens.ToString(), Settings.Default.NameFont, Brushes.Black,
                    Middle.X - s.Width / 2, Middle.Y - s.Height / 2 );
            }

            base.Draw( g );
        }

        private Place Place { get { return Entity as Place; } }

        public override Size MinSize
        {
            get
            {
                return size;
            }
        }

        public override Anchor CreateConnectionHandle()
        {
            return new PlaceShapeAnchor( this );
        }

    }
}
